const app = getApp()	
Page({
 
    /**
     * 页面的初始数据
     */
    data: {
      year:undefined,
      hasList: true,//购物车是否有数据
      totalMoney: 0,//总金额
      place:'',
      selectAllStatus: false,//是否全选
      uid: 0, //用户ID
      totalCount: 0,//数量
      houseId: 1,
      token: '',
      response: undefined,
      carts: [],
      carts1:[
        {id:2,year:2020,month:3,total:500,name:"物业管理费",type:1,isSelect:false},
        {id:1,year:2020,month:2,total:500,name:"物业管理费",type:1,isSelect:false},
        {id:0,year:2020,month:1,total:500,name:"物业管理费",type:1,isSelect:false},
        {id:5,year:2020,month:3,total:500,name:"物业管理费",type:2,isSelect:false},
        {id:4,year:2020,month:2,total:500,name:"物业管理费",type:2,isSelect:false},
        {id:3,year:2020,month:1,total:500,name:"物业管理费",type:2,isSelect:false},
      ],
    },
   
    //设置文本框值
    bindIptCartNum: function (e) {
      
      const index = e.currentTarget.dataset.index;
      var num = e.detail.value;
      let carts = this.data.carts;
      carts[index].num = num;
       
      this.setData({
        carts: carts
      });
    },
   
    
  
    
   
    //计算总价
    getTotalPrice() {
      let carts = this.data.carts;                  // 获取购物车列表
      let total = 0;
      let num = 0;
      for (let i = 0; i < carts.length; i++) {         // 循环列表得到每个数据
        if (carts[i].isSelect) {                   // 判断选中才会计算价格
          total +=  carts[i].due;    // 所有价格加起来
          num ++;
        }
      }
      this.setData({                                // 最后赋值到data中渲染到页面
        carts: carts,
        totalCount: num,
        totalMoney: total.toFixed(2)
      });
    },
   
    //绑定单选
    bindCheckbox: function (e) {
      var that = this;
      const idx = e.currentTarget.dataset.index;
      let carts = that.data.carts;
      const isSelect1 = carts[idx].isSelect;
      carts[idx].isSelect = !isSelect1;
      that.setData({
        carts: carts,
        selectAllStatus: false
      });
      that.getTotalPrice();
    },
   
   
    //绑定多选
    bindSelectAll: function (e) {
      var that = this;
      let selectedAllStatus = that.data.selectAllStatus;
      let carts = that.data.carts;
      selectedAllStatus = !selectedAllStatus;
      for (var i = 0; i < carts.length; i++) {
        carts[i].isSelect = selectedAllStatus;
      }
   
      that.setData({
        carts: carts,
        selectAllStatus: selectedAllStatus
      });
      that.getTotalPrice();
    },
   
    //购物车结算
    bindjiesuan: function () {
      var that = this;
      let carts = that.data.carts;
      let jscart1 = [];
      var j = 0
      for (var i = 0; i < carts.length; i++) {
        if (carts[i].isSelect) {
          jscart1[j] = carts[i];
          j++;
        }
      }
      if (jscart1.length <= 0) {
        wx.showToast({
          title: '未选择商品',
          icon: 'success',
          duration: 1000
        })
        return;
      }
      wx.setStorageSync('jscart1', jscart1);//存入缓存
      //转到结算页面
      wx.navigateTo({
        url: '../pay/pay2/pay2?totalMoney=' + that.data.totalMoney+'&&place='+that.data.place
      });
   
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
      var that = this;
      that.data.token = app.getToken();
      var year = options.year;
      //console.log(year);
      var houseId = that.data.houseId;
      //网络请求获取账单数据
      wx.request({
        url: 'http://localhost:8080/community/pay/listdetail?houseId='+houseId+'&&year='+year,
        method:'GET',
        header:{
          'content-type': 'application/json',
          'Authorization':  that.data.token
        },
        success:function(res){
          // console.log("userInfo" + JSON.stringify(res))
          console.log(res.data);
          that.setData({
            carts: res.data.data,
            year: year,
            place: options.place
          });
        },
      }) 
    },

   
   
    //加载购物车
    getCart: function () {
      
      
    },
    bindMingxi:function(e) {
      var idx = e.currentTarget.dataset.idx
      console.log(idx)
      var a = this.data.carts[idx]
      var content = JSON.stringify(a);
      //var year = this.data.carts[index].year
        wx.navigateTo({
          url: '../content/content?content='+content,
        })
    }

    
  })